package com.aifeng.hassan_food.mine

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.aifeng.base.BaseActivity
import com.aifeng.common_ui.base.clear
import com.aifeng.common_ui.helper.*
import com.aifeng.hassan_food.BuildConfig
import com.aifeng.hassan_food.MainActivity
import com.aifeng.hassan_food.R
import com.aifeng.hassan_food.login.LoginActivity
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.subscribeBy
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.SettingViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.activity_setting_login.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class SettingLoginActivity : BaseActivity() {

    private var mCode: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting_login)
        initTitle(title = "修改登录密码")
    }

    override fun getViewModel() = getViewModel<SettingViewModel>()

    @SuppressLint("SetTextI18n")
    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_yzm -> {
                getViewModel().getCode(getString("phone"), "2")
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = { inner ->
                            toast("已发送短信验证码至当前账号的手机上，请注意查收！")
                            mCode = inner

                            @Suppress("ConstantConditionIf")
                            if (BuildConfig.LOG_DEBUG) {
                                login_yzm.setText(mCode)
                                login_yzm.setSelection(login_yzm.text.length)
                            }

                            getViewModel().countDown(count = 60)
                                .bindLifeCycle(getLifecycleOwner())
                                .subscribeBy(
                                    onNext = {
                                        bt_yzm.text = "${60 - it}s后重试"
                                        bt_yzm.isClickable = false
                                    },
                                    onComplete = {
                                        bt_yzm.text = "重新获取"
                                        bt_yzm.isClickable = true
                                    }
                                )
                        }
                    )
            }
            R.id.bt_sure -> {
                when {
                    login_pwd.text.isBlank() -> {
                        login_pwd.requestFocus()
                        toast("请输入原密码")
                        return
                    }
                    login_yzm.text.isBlank() -> {
                        login_yzm.requestFocus()
                        toast("请输入验证码")
                        return
                    }
                    login_new.text.isBlank() -> {
                        login_new.requestFocus()
                        toast("请输入新密码")
                        return
                    }
                    login_confirm.text.isBlank() -> {
                        login_confirm.requestFocus()
                        toast("请再次输入新密码")
                        return
                    }
                    !login_pwd.text.isPasswordWithAndNumber() -> {
                        toast("请输入6~20位字母加数字组合原密码")
                        return
                    }
                    mCode.isEmpty() -> {
                        toast("请点击获取验证码")
                        return
                    }
                    login_yzm.textString() != mCode -> {
                        login_yzm.requestFocus()
                        login_yzm.clear()
                        toast("请输入正确的验证码")
                        return
                    }
                    !login_new.text.isPasswordWithAndNumber() -> {
                        toast("请输入6~20位字母加数字组合新密码")
                        return
                    }
                    login_new.textString() != login_confirm.textString() -> {
                        toast("新密码输入不一致，请重新输入")
                        return
                    }
                }

                getViewModel().editPwd(
                    "userid" to getString("userId"),
                    "pwd" to login_pwd.textString(),
                    "code" to login_yzm.textString(),
                    "newpwd" to login_new.textString(),
                    "type" to "1"
                )
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = {
                            toast("登录密码修改成功！")

                            clearShared(
                                "userId",
                                "nickName",
                                "shopName",
                                "headImg",
                                "shopId",
                                "balance",
                                "openId",
                                "realName",
                                "bankCard",
                                "bindId",
                                "bindImg",
                                "bindName",
                                "createData"
                            )
                            isLogin = false

                            LiveEventBus.get("loginStatus").post("quit")
                            LiveEventBus.get("mainLogin").post("quit")
                            ActivityStack.screenManager.popAllActivityExceptOne(MainActivity::class.java)

                            startActivity<LoginActivity>()
                        }
                    )
            }
        }
    }
}
